package com.example.qihuibao.utils

import android.app.Activity
import android.app.AlarmManager
import android.app.PendingIntent
import android.content.Context
import android.view.View


/**
 *@author: cww
 *@desc:平时一些无处安放的新的util方法可以放此处
 *@date: 2023/7/3 18:00
 */
object KTCommonUtils {
    private var sn: String? = null

    fun hideBottomUIMenu(activity: Activity) {
        //隐藏虚拟按键，并且全屏
        //for new api versions.
//        View decorView = getWindow().getDecorView();
//        int uiOptions= View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY | View.SYSTEM_UI_FLAG_FULLSCREEN;
//        decorView.setSystemUiVisibility(uiOptions);
        //禁止滑动显示
        val _window = activity.window
        val params = _window.attributes
        params.systemUiVisibility =
            View.SYSTEM_UI_FLAG_HIDE_NAVIGATION or View.SYSTEM_UI_FLAG_IMMERSIVE
        _window.attributes = params
    }

    fun restartApp(context: Context) {
        val intent = context.packageManager.getLaunchIntentForPackage(context.packageName)
        val restartIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_ONE_SHOT)
        val mgr = context.getSystemService(Context.ALARM_SERVICE) as AlarmManager
        mgr[AlarmManager.RTC, System.currentTimeMillis() + 1000] = restartIntent // 1秒钟后重启应用
        System.exit(0)
    }

    fun getDeviceSN(): String {
        if (sn == null) {
            try {
                val c = Class.forName("android.os.SystemProperties")
                val get = c.getMethod("get", String::class.java)
                sn = get.invoke(c, "ro.serialno") as String
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return sn?:""
    }

    fun isHttpOrHttps(url:String) :Boolean {
        val reg = "/(http|https):\\/\\/([\\w.]+\\/?)\\S*/"
        return url.matches(Regex(reg))
    }

    fun upgradeApp() {

    }

}